#encoding=utf-8
'''
Created on 2013-2-19

@author: Administrator
'''
from base.common import toDict, retSuccess, retHtml, createBaseHtml, \
    getQueryHtml, getSelectText, createHtml
from base.dbCommon import getPage, getLimit, convertToArray
from config.db.models import Component
import json

formJson=None

class base():
    '''
    classdocs
    '''


    def __init__(self):
        '''
        Constructor
        '''
    def initParam(self,request,param,handleName):
        self.request=request
        self.param=param
        self.handleName=handleName
        params={}
        for item in request.POST:
            temp=request.POST.get(item)
            if not temp=="":
                params[item]=request.POST.get(item)
        self.json=None
        if request.POST.get("json"):
            self.json=json.loads(request.POST.get("json"))
        self.request.params=params
        self.table=param['table']
    
    def getByKey(self,key):
        return self.request.GET.get(key)
    
    def getPost(self,key):
        return self.request.POST.get(key)    
        
    def query(self):
        qryName = self.getByKey("name")
        page = getPage(self.request)
        limit = getLimit(page)
        retBean = None
        if qryName:
            retBean = self.table.objects.filter(name__icontains=qryName)
        else:
            retBean = self.table.objects.all()
        total=retBean.count()
        retBean = retBean[limit.get("start"):limit.get("end")]
        return {'total':total,'rows':convertToArray(retBean.values())} 
    def create(self):
        item = self.table()
        for temp in self.request.params:
            item.setAttr(temp, self.request.params.get(temp))
        item.save()
        return toDict(item)
    
    def delRow(self):
        modId = self.getPost("id")
        self.table.objects.get(id=modId).delete()
        return retSuccess("删除成功")
    
    def getById(self):
        retBean = self.table.objects.get(id=self.getPost("id"))
        return toDict(retBean);
    
    def editForm(self):
        return retHtml('editForm',None)
    
    def toHtml(self,name):
        return retHtml(name,None)
    
    def gotoEdit(self):
        
        modId = self.getByKey("id")
        
        return createBaseHtml(self.formJson)+getQueryHtml({'url':'/do?action='+self.handleName+'.getById','id':modId})
    
    def add(self):
        return createBaseHtml(self.formJson)   
    
    def queryAll(self):
        return convertToArray(self.table.objects.all().values())   
    
    def detail(self):
        id=self.getPost("id")
        item=self.table.objects.get(id=id)
        cfg=[]
        for field in self.formJson:
            if not field.has_key("id") or field['key']=='Hidden':continue
            val=item[field['id']]
            if field['key']=='Select' and field.has_key("subs"):
                val=getSelectText(field.get("subs"),val)
            key='Detail'
            if field.has_key("detail_key"):
                key=field.get("detail_key")  
            cfg.append({'key':key,'content':val,'name':field['name']})
        
        return createHtml(cfg)   
        